package com.spring.security.core.validate;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * 短信验证码
 *
 * @author seabed_moon
 */
@Setter
@Getter
@ToString
public class ValidateCode implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1588203828504660915L;

    private String mobile;

    private String code;

    @DateTimeFormat
    private LocalDateTime expireTime;

    public ValidateCode(String code, int expireIn) {
        this.code = code;
        this.expireTime = LocalDateTime.now().plusSeconds(expireIn);
    }

    public ValidateCode(String code, LocalDateTime expireTime) {
        this.code = code;
        this.expireTime = expireTime;
    }

    public ValidateCode(String mobile, String code, int expireIn) {
        this.mobile = mobile;
        this.code = code;
        this.expireTime = LocalDateTime.now().plusSeconds(expireIn);
    }

    /**
     * 获取剩余秒数
     *
     * @return
     */
    public Long surplusSecond() {
        return ChronoUnit.SECONDS.between(LocalDateTime.now(), this.expireTime);
    }

    /**
     * 是否过期
     *
     * @return
     */
    public boolean isExpire() {
        return LocalDateTime.now().isAfter(expireTime);
    }
}
